package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constants.ItemMqConstants;
import com.hmall.item.pojo.Item;

import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("item")
//@Transactional
public class ItemController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IItemService itemService;

    // 分页查询
    @GetMapping("/list")
    public PageDTO<Page> page(int page, int size) {
        Page<Item> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(Item::getCreateTime);
        itemService.page(pageInfo, qw);
        return new PageDTO(pageInfo.getTotal(), pageInfo.getRecords());

    }

    // 添加
    @PostMapping
    public void save(@RequestBody Item item) {
        Item item1 = new Item();
        BeanUtils.copyProperties(item, item1);
        Date date = new Date(System.currentTimeMillis());
        item1.setStatus(2);
        item1.setCreateTime(date);
        item1.setUpdateTime(date);
        itemService.save(item1);

    }

    // 修改状态
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable String id, @PathVariable String status) {
        LambdaUpdateWrapper<Item> qw = new LambdaUpdateWrapper<>();
        Date date = new Date(System.currentTimeMillis());
        qw.set(Item::getUpdateTime,date);
        qw.set(Item::getStatus, status);
        qw.in(Item::getId, id);
        itemService.update(qw);
        rabbitTemplate.convertAndSend("item.topic","item.up", id);
//        rabbitTemplate.convertAndSend(ItemMqConstants.EXCHANGE_NAME,ItemMqConstants.INSERT_KEY,id);


    }

    // 更新
    @PutMapping
    public void update(@RequestBody Item item) {

        itemService.updateById(item);

    }

    //删除
    @DeleteMapping("/{ids}")
    public void delete(@PathVariable String ids) {
        itemService.removeById(ids);

//        rabbitTemplate.convertAndSend(ItemMqConstants.EXCHANGE_NAME,ItemMqConstants.DELETE_KEY,ids);
    }



    // 根据id查询
    @GetMapping("/{id}")
    public Item get(@PathVariable Long id) {
        Item Item = itemService.getById(id);
        return Item;
    }

    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable("itemId")Long itemid,@PathVariable("num")Integer num){
        itemService.updateStock(itemid,num);
    }


    @PutMapping("/stocks/{itemId}/{num1}")
    public void updateStockRecover(@PathVariable("itemId")Long itemId,@PathVariable("num1")Integer num1){
        itemService.updateStockRecover(itemId,num1);
    }


}